<template>
  <div style="height:100%;width:100%;">
    <div id="barLineCanvas" style="height:100%;width:100%;" ref="barLineCanvas"></div>
  </div>
</template>

<script>
export default {
  name: "barLineCanvas",
  data() {
    return {};
  },
  props: {
    barData: {
      type: Object,
    },
    id: {
      type: String,
    },
  },
  watch: {
    barData(val) {
      this.init(val);
    },
  },
  mounted() {
    // this.init();
  },
  methods: {
    init(val) {
      var xdata = [];
      var obj = {};
      switch (this.id) {
        case "tabs1-1":
          xdata = val.areaName;
          obj = obj = {
            本年投资: {
              type: "bar",
              data: val.investLjComple,
            },
            增量收入: {
              type: "bar",
              data: val.increaseIncome,
            },
            百元投资拉动增量收入: {
              type: "line",
              data: val.bytzld,
            },
          };
          break;
        case "tabs1-2":
          xdata = val.indexName;
          obj = obj = {
            本年投资: {
              type: "bar",
              data: val.investLjComple,
            },
            增量收入: {
              type: "bar",
              data: val.increaseIncome,
            },
            百元投资拉动增量收入: {
              type: "line",
              data: val.bytzld,
            },
          };
          break;
        case "tabs2":
          xdata = val.xdata;
          obj = obj = {
            画饼: {
              type: "bar",
              data: val.barData1,
            },
            下达: {
              type: "bar",
              data: val.barData2,
            },
            下达率: {
              type: "line",
              data: val.lineData,
            },
          };
          break;

        default:
          break;
      }

      let legendata = Object.keys(obj);

      var myChart = this.$echarts.init(this.$refs.barLineCanvas);
      let option = {
        color: [
          //自定义的颜色
          new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "#49DFFE" },
            { offset: 1, color: "#41A2FD" }, //柱图渐变色
          ]),
          new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: "#FCD449" },
            { offset: 1, color: "#FA8388" }, //柱图渐变色
          ]),
          "#F3707C",
        ],
        tooltip: {
          trigger: "axis",
          axisPointer: {
            type: "cross",
            crossStyle: {
              color: "#999",
            },
          },
        },

        grid: {
          left: "1%",
          right: "1%",
          bottom: "10%",
          top: "13%",
          containLabel: true,
        },
        legend: {
          data: legendata,
          right: "3%",
          top: "4%",
          // itemWidth: 12,
          itemHeight: 12,
          itemGap: 20,
          textStyle: {
            fontSize: 12,
          },
        },
        xAxis: [
          {
            type: "category",
            data: xdata,
            axisPointer: {
              type: "shadow",
            },
            axisLabel: {
              show: true,
              rotate: 30,
              // textStyle: {
              //   color: "#000",
              //   fontSize: 12,
              // },
            },
            axisLine: {
              show: false,
            },
            axisTick: {
              show: false,
            },
            // axisLine: {
            //   lineStyle: {
            //     color: "#eee",
            //     width: 1, // 粗细
            //   },
            // },
          },
        ],
        yAxis: [
          {
            // type: this.id == 'tabs1-1' ? "log" : 'value',
            type: 'value',
            name: "单位：万元",
            nameTextStyle: {
              fontSize: 14,
              padding: [0, 0, 5, 50],
              color: "rgba(0, 0, 0, 0.4)",
            },
            axisLine: {
              lineStyle: {
                color: "#eee",
                width: 1, // 粗细
              },
            },
            axisLabel: {
              show: true,

              textStyle: {
                color: "#000",
                fontSize: 12,
              },
            },
            axisTick: {
              show: false,
            },
          },
          {
            type: "value",
            axisLine: {
              lineStyle: {
                color: "#eee",
                width: 1, // 粗细
              },
            },
            splitLine: {
              //网格线
              show: false,
            },
            axisLabel: {
              show: true,
              textStyle: {
                color: "#000",
                fontSize: 12,
              },
            },
            axisTick: {
              show: false,
            },
          },
        ],
        series: [
          {
            name: legendata[0],
            type: "bar",
            label: {
              show: true,
              position: "top",
              rotate: 30,
            },
            barMaxWidth: "18px",

            // barGap: 0,
            data: obj[legendata[0]].data,
          },
          {
            name: legendata[1],
            type: "bar",
            // barGap: 0,
            label: {
              show: true,
              position: "top",
              rotate: 30,
            },
            barMaxWidth: "18px",
            data: obj[legendata[1]].data,
          },
          {
            name: legendata[2],
            type: "line",
            yAxisIndex: 1,
            smooth: false,
            lineStyle: {
              color: "#F3707C",
              width: 3,
            },
            data: obj[legendata[2]].data,
          },
        ],
      };
      myChart.setOption(option);
      setTimeout(function () {
        window.addEventListener("resize", () => {
          myChart.resize();
        });
      }, 500);
    },
  },
};
</script>

<style>
</style>
